from odoo import fields, models, api, Command, _
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare, float_is_zero


class WorkorderScrapRecord(models.Model):
    _name = 's.workorder.scrap.record'
    _description = 'Work order Scrap Record'

    workorder_id = fields.Many2one('mrp.workorder', 'work order')
    discover_workcenter_id = fields.Many2one('mrp.workcenter', string='Discover Process', related='workorder_id.workcenter_id')
    production_id = fields.Many2one('mrp.production', string='Production', related='workorder_id.production_id', store=True)
    quality_alert_id = fields.Many2one('quality.alert', 'quality alert')
    scrap_workorder_id = fields.Many2one('mrp.workorder', 'responsibility work')
    scrap_workcenter_id = fields.Many2one('mrp.workcenter', string='Responsibility Process', copy=False, required=True)
    scrap_work_station_id = fields.Many2one('work.station', string="Work Station", related='scrap_workcenter_id.work_station_id')
    scrap_work_station_ids = fields.Many2many(
        'work.station',
        compute='_compute_scrap_work_station_ids',
        compute_sudo=True
    )
    scrap_reason_id = fields.Many2one('scrap.reason', string="Scrap Reason", required=True)
    scrap_qty = fields.Integer('Scrap Quantity')
    scrap_set_qty = fields.Integer('Scrap SET Quantity')
    scrap_panel_qty = fields.Integer('Scrap PANEL Quantity')

    writable_field = fields.Selection([
        ('a', 'all'),
        ('q', 'qty'),
        ('s', 'set'),
        ('p', 'panel'),
    ], default='a')
    stage_id = fields.Many2one(related='quality_alert_id.stage_id', string='State')

    @api.depends('scrap_reason_id')
    def _compute_scrap_work_station_ids(self):
        for rec in self:
            if rec.scrap_reason_id:
                rec.scrap_work_station_ids = rec.scrap_reason_id.work_station_ids
            else:
                rec.scrap_work_station_ids = [Command.clear()]

    @api.onchange('scrap_panel_qty')
    def onchange_scrap_panel_qty(self):
        scrap_qty = self.scrap_panel_qty * self.quality_alert_id.product_id.value_pnl
        scrap_qty = max(min(scrap_qty, self.quality_alert_id.s_workorder_id.qty_wip), 0)

        if self.quality_alert_id.product_id.value_set:
            scrap_set_qty = self.scrap_panel_qty * self.quality_alert_id.product_id.value_pnl / self.quality_alert_id.product_id.value_set
        else:
            scrap_set_qty = 0
        scrap_set_qty = max(min(scrap_set_qty, self.quality_alert_id.s_workorder_id.qty_wip_set), 0)
        self.write({
            'scrap_qty': scrap_qty,
            'scrap_set_qty': scrap_set_qty,
            'writable_field': 'p'
        })
        # rounding = self.quality_alert_id.s_production_id.product_uom_id.rounding or 0.01
        # if float_is_zero(self.scrap_qty, precision_rounding=rounding) and \
        #         float_is_zero(self.scrap_set_qty, precision_rounding=rounding) and \
        #         float_is_zero(self.scrap_set_qty, precision_rounding=rounding):
        #     self.writable_field = 'a'

    @api.onchange('scrap_set_qty')
    def onchange_scrap_set_qty(self):
        scrap_qty = self.scrap_set_qty * self.quality_alert_id.product_id.value_set
        self.scrap_qty = max(min(scrap_qty, self.quality_alert_id.s_qty_wip), 0)
        # if self.writable_field == 'a':
        #     self.writable_field = 's'
        #
        # rounding = self.quality_alert_id.s_production_id.product_uom_id.rounding or 0.01
        # if float_is_zero(self.scrap_qty, precision_rounding=rounding) and \
        #         float_is_zero(self.scrap_set_qty, precision_rounding=rounding) and \
        #         float_is_zero(self.scrap_set_qty, precision_rounding=rounding):
        #     self.writable_field = 'a'

    # @api.onchange('scrap_qty')
    # def onchange_scrap_qty(self):
    #     if self.writable_field == 'a':
    #         self.writable_field = 'q'
    #     rounding = self.quality_alert_id.s_production_id.product_uom_id.rounding or 0.01
    #     if float_is_zero(self.scrap_qty, precision_rounding=rounding) and \
    #             float_is_zero(self.scrap_set_qty, precision_rounding=rounding) and \
    #             float_is_zero(self.scrap_set_qty, precision_rounding=rounding):
    #         self.writable_field = 'a'


class WorkorderReturnScrapRecord(models.Model):
    _name = 's.workorder.return.scrap.record'
    _description = 'Workorder Return Scrap Record'

    workorder_id = fields.Many2one('mrp.workorder', 'work order')
    discover_workcenter_id = fields.Many2one('mrp.workcenter', string='Discover Process', related='workorder_id.workcenter_id')
    production_id = fields.Many2one('mrp.production', string='Production', related='workorder_id.production_id', store=True)
    scrap_workorder_id = fields.Many2one('mrp.workorder', 'responsibility work')
    scrap_workcenter_id = fields.Many2one('mrp.workcenter', string='Responsibility Process', copy=False)
    scrap_work_station_id = fields.Many2one('work.station', string="Work Station", related='scrap_workcenter_id.work_station_id')
    scrap_qty = fields.Integer('Return Scrap Quantity')
    scrap_set_qty = fields.Integer('Return Scrap SET Quantity')
    scrap_panel_qty = fields.Integer('Return Scrap PANEL Quantity')
    note = fields.Char(string='note')
    # ===============
    scrap_reason_id = fields.Many2one('scrap.reason', string="Return Scrap Reason")
    quality_alert_id = fields.Many2one('quality.alert', 'quality alert')

